package ca.nfb.media.nfbPlayer.views
{
	import ca.turbulent.media.Pyro;
	import ca.nfb.media.nfbPlayer.NFBPyroBrew;
	import ca.turbulent.media.events.StatusUpdateEvent;
	import ca.turbulent.media.events.PyroEvent;
	import ca.nfb.media.nfbPlayer.ui.ToolTip;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.FullScreenEvent;
	import flash.filters.*;
	import flash.display.MovieClip;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	import ca.nfb.media.nfbPlayer.ComModule;	
		
		
	public class InsufficientBandwidthView extends NFBPlayerView
	{
		
		private var messages			:Array = new Array();
		
		public function InsufficientBandwidthView()
		{
			super();
			this.visible = false;
			player.addEventListener(PyroEvent.INSUFFICIENT_BANDWIDTH, insufficientBandwidth, false, 0, true);
			player.addEventListener(PyroEvent.BANDWIDTH_CHECKED, bandwidthChecked, false, 0, true);
			player.addEventListener(PyroEvent.BUFFER_TIME_ADJUSTED, bufferTimeAdjusted, false, 0, true);
			player.addEventListener(NFBPyroBrew.DETECTED_SPEED_2_LOW_FOR_QUALITY, insufficientBandwidth, false, 0, true);
			
			messages['FR'] = new Array();
			messages['EN'] = new Array();
			
			messages['FR'].push("Votre vitesse de connection semble trop basse pour la qualié du flux vidéo que vous tentez présentement de visionner.<br><br> Pour une meilleur expérience sur notre site, nous vous conseillons fortement de reprendre votre visionement en choisissant une qualité moindre.");
			messages['FR'].push("Votre vitesse de connection semble trop basse pour la qualié du flux vidéo que vous tentez présentement de visionner.<br><br> Pour une meilleur expérience sur notre site, nous tenterons d'ajuster le téléchargement pour optimiser votre visionement.");
			messages['EN'].push("Your internet connection speed seems too slow for the video quality you are currently trying to view.<br><br>For a better experience on our site, we strongly recomend switching to a lower quality feed.");
			messages['EN'].push("Your internet connection speed seems too slow for the video quality you are currently trying to view.<br><br>For a better experience on our site, we will try to optimise the buffering settings to give you the best viewing experience possible.");
			
			changeQualityButton_mc.useHandCursor = true;
			changeQualityButton_mc.mouseChildren = false;
			cancelButton_mc.useHandCursor = true;
			cancelButton_mc.mouseChildren = false;
			
			changeQualityButton_mc.addEventListener(MouseEvent.MOUSE_OVER, mOver);
			cancelButton_mc.addEventListener(MouseEvent.MOUSE_OVER, mOver);
			
			changeQualityButton_mc.addEventListener(MouseEvent.MOUSE_OUT, mOut);
			cancelButton_mc.addEventListener(MouseEvent.MOUSE_OUT, mOut);
			
			changeQualityButton_mc.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
			cancelButton_mc.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
			
			changeQualityButton_mc.addEventListener(MouseEvent.MOUSE_UP, changeUp);
			cancelButton_mc.addEventListener(MouseEvent.MOUSE_UP, cancelUp);
			
			if (ComModule.getLang() == "FR")
			{
				cancelButton_mc.label_mc.gotoAndStop(2);
				changeQualityButton_mc.label_mc.gotoAndStop(2)
			}
		}
		
		private function mOver(evt:MouseEvent):void
		{
			MovieClip(evt.currentTarget).gotoAndStop(2);
		}
		
		private function mOut(evt:MouseEvent):void
		{
			MovieClip(evt.currentTarget).gotoAndStop(1);
		}
		
		private function mDown(evt:MouseEvent):void
		{
			MovieClip(evt.currentTarget).gotoAndStop(2);
		}
		
		private function changeUp(evt:MouseEvent):void
		{
			MovieClip(evt.currentTarget).gotoAndStop(1);
			close();
			core.setCurrentState(NFBPlayer.OPTIONS_STATE);
			
		}
		
		private function cancelUp(evt:MouseEvent):void
		{
			MovieClip(evt.target).gotoAndStop(1);
			close();
		}
		
		private function insufficientBandwidth(evt:Event):void
		{
			player.pause();
			if (player.connectionSpeed == Pyro.CONNECTION_SPEED_MEDIUM)
			{
				if (player.streamQuality == Pyro.CONNECTION_SPEED_HIGH)
				{
					if (!player.changeConnectionWarned && (ComModule.getInstance().latestMovieHasNormalSpeed || ComModule.getInstance().latestMovieHasLowSpeed))
					{
						player.changeConnectionWarned = true;
						show(messages[ComModule.getLang()][0]);
						changeQualityButton_mc.mouseEnabled = true;
						changeQualityButton_mc.alpha = 1;
					}
					else
					{
						if (!player.lowBandwidthWarned)
						{
							player.lowBandwidthWarned = true;
							show(messages[ComModule.getLang()][1]);
							changeQualityButton_mc.mouseEnabled = false;
							changeQualityButton_mc.alpha = .5;	
						}
						
					} 
				}
				else if (player.streamQuality == Pyro.CONNECTION_SPEED_MEDIUM)
				{
					if (!player.changeConnectionWarned && ComModule.getInstance().latestMovieHasLowSpeed)
					{
						player.changeConnectionWarned = true;
						show(messages[ComModule.getLang()][0]);
						changeQualityButton_mc.mouseEnabled = true;
						changeQualityButton_mc.alpha = 1;
					}
					else
					{
						if (!player.lowBandwidthWarned)
						{
							player.lowBandwidthWarned = true;
							show(messages[ComModule.getLang()][1]);
							changeQualityButton_mc.mouseEnabled = false;
							changeQualityButton_mc.alpha = .5;
						}	 
					}
				}
			}	
			else if (player.connectionSpeed == Pyro.CONNECTION_SPEED_LOW)
			{
				if (player.streamQuality == Pyro.CONNECTION_SPEED_HIGH || player.streamQuality == Pyro.CONNECTION_SPEED_MEDIUM)
				{
					if (!player.changeConnectionWarned && ComModule.getInstance().latestMovieHasLowSpeed)
					{
						player.changeConnectionWarned = true;
						show(messages[ComModule.getLang()][0]);
						changeQualityButton_mc.mouseEnabled = true;
						changeQualityButton_mc.alpha = 1;
					}
					else
					{
						if (!player.lowBandwidthWarned)
						{
							player.lowBandwidthWarned = true;
							show(messages[ComModule.getLang()][1]);
							changeQualityButton_mc.mouseEnabled = false;
							changeQualityButton_mc.alpha = .5;
						} 
					}
				} 
			}
		}
		
		private function bufferTimeAdjusted(evt:PyroEvent):void
		{
			trace("ADJUSTING BUFFER TIME TO "+player.bufferTime);
		}
		
		private function bandwidthChecked(evt:PyroEvent):void
		{
			
		}
		
		
		public function closeView():void
		{
			this.visible = false;
		}
		
		public function show(msg:String):void
		{
			this.x = (stage.stageWidth - this.width) / 2;
			this.y = ((stage.stageHeight-core.controllerHeight) - this.height) / 2;
			this.message_txt.htmlText = msg;
			this.visible = true;
		}
		
	}
}